package org.microservice.system.controller;

import org.microservice.common.base.BaseControllerImpl;
import org.microservice.common.bean.RoleVo;
import org.microservice.common.util.PageUtils;
import org.microservice.common.util.Query;
import org.microservice.common.util.R;
import org.microservice.system.dao.SysRoleDao;
import org.microservice.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/6/20
 * Time: 15:54
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseControllerImpl<SysRoleDao,RoleVo> {

    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("/getAllRoles")
    public R getAllRoles(){
        List<RoleVo> roleVos=  sysRoleService.getAllRoles();

        return R.ok().put("data",roleVos);
    }

    /**
     * 由用户id查询角色
     * @param id
     * @return
     */
    @GetMapping("/getUserRoleByUserId/{id}")
    public R getUserRoleByUserId(@PathVariable(value = "id") int id){

        List<RoleVo> roleVos = sysRoleService.getUserRoleByUserId(id);

        return R.ok().put("data",roleVos);
    }




    public R getListPage(@RequestBody Map<String,Object> params){
        Query  query = new Query(params);

        List<RoleVo> roleVos = sysRoleService.getRoleListPage(query);

        int total= sysRoleService.getRoleListPageTotal(params);

        PageUtils pageUtils = new PageUtils(roleVos, total, query.getLimit(), query.getCurrPage());
        return R.ok().put("data",pageUtils);

    }

    public R deleteById(@RequestParam(value = "id") Serializable roleId){

        R r=sysRoleService.deleteRole(roleId);
        return r;
    }

    /**
     * 角色的菜单授权
     * @param roleVo
     * @return
     */
    @PostMapping("/assignMenus")
    public R assignMenus(@RequestBody RoleVo roleVo){
        sysRoleService.assignMenus(roleVo);
        return R.ok();
    }



}
